package edu.gdin.ecommerce.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import edu.gdin.ecommerce.model.Role;

@Repository("roleDAO")
public class RoleDAO extends BaseDAO {

	// Role中对Func为LAZY，只有个别情况下需要加载Func，可使用该方法！
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Role> getAll() {
		List<Role> roles = (List<Role>) this.list(Role.class);
		
		Session s = this.getHibernateTemplate().getSessionFactory().openSession();
		s.beginTransaction();
		for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
			Role role = (Role) iterator.next();
			Role r = (Role) s.get(Role.class, role.getRoleId());
			System.out.println("each role's funcs'size:" + r.getFuncs().size());
			role.setFuncs(r.getFuncs());
		}
		
//		List<Role> roles = (List<Role>) this.list(Role.class);
//		for (Iterator iterator = roles.iterator(); iterator.hasNext();) {
//			Role role = (Role) iterator.next();
//			Role r = (Role) this.findByPk(Role.class, role.getRoleId());
//			System.out.println("each role's funcs'size:" + r.getFuncs().size());
//			role.setFuncs(r.getFuncs());
//		}
		return roles;
	}
	
}
